<?php
include_once 'protoweb_controller.php';

/**--------------------------------------------------------------------------
 * Please, don't touch this file.
 *
 * If you need to custom this Class, you can edit recipients.php
 * file overwrite and define new methods
 *---------------------------------------------------------------------------
 */

/**
 * @package Protoweb
 * @subpackage controller
 */
class Recipients_protoweb extends Protoweb_controller {

  public function __construct() {
    parent::__construct();
  } // end of method __construct()



  public function edit($id=0) {
    $data["recipient"] = new Recipient($id);

    if ($this->input->post("posting") == 1)
      $data = $this->edit_save($data["recipient"]->id);

    // To one relationships
        
    $clients = new Client();
    $data["clients"] = $clients->all_to_asoc_array("id","name");
    $data["clients_selected"] = $data["recipient"]->client->id;
    
    // To many relationships
    
    $subscribers = new Subscriber();
    $data["subscribers"] = $subscribers->all_to_asoc_array("id","subscribed");
    $data["subscribers_selected"] = $data["recipient"]->get_relationship_ids("subscriber");
    
    $sends = new Send();
    $data["sends"] = $sends->all_to_asoc_array("id","read");
    $data["sends_selected"] = $data["recipient"]->get_relationship_ids("send");
    
    $attrvalues = new Attrvalue();
    $data["attrvalues"] = $attrvalues->all_to_asoc_array("id","value");
    $data["attrvalues_selected"] = $data["recipient"]->get_relationship_ids("attrvalue");
    
    $this->smarty_parser->view('recipient_edit.html', $data);
  } // end of method edit($id=0)


  protected function edit_save($id) {
    $data["error"] = "";
    $data["recipient"] = new Recipient($id);

          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->email = $this->input->post("email");
      if ($data["recipient"]->email == "") $data["recipient"]->email = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->email->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(email)) $data["recipient"]->email = $fileupload.clean_file_name(email);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->firstname = $this->input->post("firstname");
      if ($data["recipient"]->firstname == "") $data["recipient"]->firstname = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->firstname->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(firstname)) $data["recipient"]->firstname = $fileupload.clean_file_name(firstname);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->lastname = $this->input->post("lastname");
      if ($data["recipient"]->lastname == "") $data["recipient"]->lastname = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->lastname->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(lastname)) $data["recipient"]->lastname = $fileupload.clean_file_name(lastname);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->nickname = $this->input->post("nickname");
      if ($data["recipient"]->nickname == "") $data["recipient"]->nickname = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->nickname->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(nickname)) $data["recipient"]->nickname = $fileupload.clean_file_name(nickname);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->created = $this->input->post("created");
      if ($data["recipient"]->created == "") $data["recipient"]->created = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->created->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(created)) $data["recipient"]->created = $fileupload.clean_file_name(created);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->verified = $this->input->post("verified");
      if ($data["recipient"]->verified == "") $data["recipient"]->verified = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->verified->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(verified)) $data["recipient"]->verified = $fileupload.clean_file_name(verified);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->offdate = $this->input->post("offdate");
      if ($data["recipient"]->offdate == "") $data["recipient"]->offdate = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->offdate->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(offdate)) $data["recipient"]->offdate = $fileupload.clean_file_name(offdate);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->offreason = $this->input->post("offreason");
      if ($data["recipient"]->offreason == "") $data["recipient"]->offreason = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->offreason->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(offreason)) $data["recipient"]->offreason = $fileupload.clean_file_name(offreason);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->sents = $this->input->post("sents");
      if ($data["recipient"]->sents == "") $data["recipient"]->sents = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->sents->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(sents)) $data["recipient"]->sents = $fileupload.clean_file_name(sents);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
          // Assign value to field. If value is empty, we will assign NULL
      $data["recipient"]->bounceds = $this->input->post("bounceds");
      if ($data["recipient"]->bounceds == "") $data["recipient"]->bounceds = NULL;
      
      // files for upload
      /*
      if ($data["recipient"]->bounceds->pw_datatype->id == 8) {
        $fileupload = new Upload();
        if ($fileupload.do_upload(bounceds)) $data["recipient"]->bounceds = $fileupload.clean_file_name(bounceds);
        else die(var_dump($fileupload.error_msg));
      }
      */
      
    
    //saving attributes
    if(! $data["recipient"]->save())
      $data["error"] .= $data["recipient"]->error->string;

    //saving to one relationships
    
    $client = new Client($this->input->post("client"));
    if (! $data["recipient"]->save(array($client)))
      $data["error"] .= $data["recipient"]->error->string;
    
    //to many relationships
                            
    redirect(base_url()."recipients/edit/".$data["recipient"]->id);
  } // end of method edit_save($id)


  public function delete($id) {
    $data["error"] = "";
    $data["recipient"] = new Recipient($id);

    //delete
    if(! $data["recipient"]->delete())
      $data["error"] .= $data["recipient"]->error->string;

    redirect(base_url()."recipients/edit/");
  } // end of method delete($id)

    
} // class Recipients_protoweb

/* End of file recipients_protoweb.php */
/* Location: ./system/application/controllers/recipients_protoweb.php */
